package bb.lanxing.databinding.unit;

import com.github.mikephil.charting.utils.Utils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public abstract class Unit {
    public static final int DEFAUT_DEVICE_HEART_MAX_VALUE = 180;
    public static final int DEFAUT_DEVICE_HEART_MIN_VALUE = 60;
    public static final int DEFAUT_DEVICE_LTHR_MAX_VALUE = 160;
    public static final int DEFAUT_DEVICE_LTHR_MIN_VALUE = 100;
    public static final int DEFAUT_HEART_MAX_VALUE = 220;
    public static final int DEFAUT_HEART_MIN_VALUE = 60;
    public static final int DEFAUT_LTHR_MAX_VALUE = 200;
    public static final int DEFAUT_LTHR_MIN_VALUE = 100;
    public static final String MEASUREMENT_PREF_BRITISH = "f";
    public static final String MEASUREMENT_PREF_METRIC = "m";
    public static final String MEASUREMENT_PREF_TEMPERATURE_BRITISH = "f";
    public static final String MEASUREMENT_PREF_TEMPERATURE_METRIC = "c";
    DecimalFormatSymbols dfs;
    DecimalFormat numberDecimalFormat;
    protected String type;
    protected String valueBritish;
    protected String valueMetric;

    @Retention(RetentionPolicy.SOURCE)
    public @interface UNIT_TYPE {
    }

    abstract String convertToBritish(String str);

    abstract String convertToBritish(String str, int i);

    String convertToBritishG2LB(String str, int i) {
        return null;
    }

    abstract String convertToMetric(String str);

    abstract String convertToMetric(String str, int i);

    String convertToMetricLB2G(String str, int i) {
        return null;
    }

    String convertToMetricSpeedMiHi2cmS(String str) {
        return null;
    }

    abstract String getUnitString(String str);

    public Unit(String str, String str2) {
        this.dfs = new DecimalFormatSymbols();
        this.numberDecimalFormat = new DecimalFormat("0.0");
        this.dfs.setDecimalSeparator('.');
        this.numberDecimalFormat.setDecimalFormatSymbols(this.dfs);
        this.type = str2;
        setUpValue(str);
    }

    public Unit(String str, String str2, DecimalFormat decimalFormat) {
        this.dfs = new DecimalFormatSymbols();
        this.numberDecimalFormat = new DecimalFormat("0.0");
        this.dfs.setDecimalSeparator('.');
        decimalFormat.setDecimalFormatSymbols(this.dfs);
        this.numberDecimalFormat = decimalFormat;
        this.type = str2;
        setUpValue(str);
    }

    public Unit setUpValue(String str) {
        if (this.type.equals("f")) {
            this.valueMetric = convertToMetric(str);
            this.valueBritish = str;
        } else if (this.type.equals("m")) {
            this.valueBritish = convertToBritish(str);
            this.valueMetric = str;
        }
        return this;
    }

    public Unit setUpValue(String str, String str2) {
        setType(str2);
        if (str2.equals("f")) {
            this.valueBritish = str;
            this.valueMetric = convertToMetric(str);
        } else if (str2.equals("m")) {
            this.valueMetric = str;
            this.valueBritish = convertToBritish(str);
        }
        return this;
    }

    public Unit setUpValueWeight(String str, String str2, int i) {
        setType(str2);
        if (str2.equals("f")) {
            this.valueBritish = str;
            this.valueMetric = convertToMetricLB2G(str, i);
        } else if (str2.equals("m")) {
            BigDecimal bigDecimal = new BigDecimal(str);
            String str3 = bigDecimal.setScale(i, 4).doubleValue() + "";
            this.valueMetric = str3;
            this.valueBritish = convertToBritish(str3);
        }
        return this;
    }

    public Unit setUpValueSpeed(String str, String str2) {
        setType(str2);
        if (str2.equals("f")) {
            this.valueBritish = str;
            this.valueMetric = convertToMetricSpeedMiHi2cmS(str);
        } else if (str2.equals("m")) {
            this.valueMetric = convertKmH2cmS(str);
            this.valueBritish = convertToBritish(str);
        }
        return this;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String str) {
        this.type = str;
    }

    public boolean isMetric() {
        return this.type.equals("m");
    }

    public String getStringValueBy(String str) {
        if (str.equals("f")) {
            return this.valueBritish;
        }
        if (str.equals("m")) {
            return this.valueMetric;
        }
        return this.valueMetric;
    }

    public double getValueBy(String str) {
        if (str.equals("f")) {
            return getValueFormatted(this.valueBritish);
        }
        if (str.equals("m")) {
            return getValueFormatted(this.valueMetric);
        }
        return getValueFormatted(this.valueMetric);
    }

    public double getValueHalfUpBy(String str, int i) {
        if (str.equals("f") || str.equals("f")) {
            return getValueHalfUpFormatted(this.valueBritish, i);
        }
        if (str.equals("m") || str.equals("c")) {
            return getValueHalfUpFormatted(this.valueMetric, i);
        }
        return getValueHalfUpFormatted(this.valueMetric, i);
    }

    public Unit setUpValue(String str, String str2, int i) {
        setType(str2);
        if (str2.equals("f") || str2.equals("f")) {
            this.valueBritish = str;
            this.valueMetric = convertToMetric(str, i);
        } else if (str2.equals("m") || str2.equals("c")) {
            this.valueMetric = str;
            this.valueBritish = convertToBritish(str, i);
        }
        return this;
    }

    public static String convertKmH2cmS(String str) {
        BigDecimal multiply = new BigDecimal(str).multiply(new BigDecimal("5")).divide(new BigDecimal("18"), 9, 4).multiply(new BigDecimal("100"));
        return multiply.setScale(0, 4).intValue() + "";
    }

    public static String convertcmS2Kmh(String str) {
        BigDecimal divide = new BigDecimal(str).multiply(new BigDecimal("3.6")).divide(new BigDecimal("100"), 9, 4);
        return divide.setScale(1, 4).doubleValue() + "";
    }

    public String getValueMetric() {
        return this.valueMetric;
    }

    public String getValueBritish() {
        return this.valueBritish;
    }

    public Double getValueFormatted(String str) {
        try {
            return Double.valueOf(this.numberDecimalFormat.format(new BigDecimal(str)));
        } catch (Exception unused) {
            Double.valueOf(str);
            return (double) Utils.DOUBLE_EPSILON;
        }
    }

    private Double getValueHalfUpFormatted(String str, int i) {
        try {
            return new BigDecimal(str).setScale(i, RoundingMode.HALF_UP).doubleValue();
        } catch (Exception e) {
            e.printStackTrace();
            return (double) Utils.DOUBLE_EPSILON;
        }
    }

    private Double getValueHalfDownFormatted(String str, int i) {
        try {
            return new BigDecimal(str).setScale(i, RoundingMode.HALF_DOWN).doubleValue();
        } catch (Exception e) {
            e.printStackTrace();
            return (double) Utils.DOUBLE_EPSILON;
        }
    }

    public static float divide(String str, String str2, int i) {
        BigDecimal bigDecimal = new BigDecimal(str);
        BigDecimal bigDecimal2 = new BigDecimal(str2);
        if (str == null) {
            bigDecimal = new BigDecimal(0);
        }
        if (str2 == null) {
            bigDecimal2 = new BigDecimal(0);
        }
        return bigDecimal.divide(bigDecimal2, i, 4).floatValue();
    }
}
